package ioc

import (
	"github.com/gin-gonic/gin"
)

type IocContainer struct {
	Ioc map[string]Object
}

func NewIocContainer() *IocContainer {
	return &IocContainer{
		Ioc: map[string]Object{},
	}
}

func (i *IocContainer) Registry(obj Object) {
	i.Ioc[obj.Name()] = obj
}

func (i *IocContainer) Get(name string) Object {
	return i.Ioc[name]
}

func (i *IocContainer) Init() {
	for _, v := range i.Ioc {
		v.Init()
	}
}

func (i *IocContainer) RouteRegistry(r gin.IRouter) {
	for _, v := range i.Ioc {
		if a, ok := v.(GinRegistry); ok {
			a.Registry(r)
		}
	}
}

type GinRegistry interface {
	Registry(r gin.IRouter)
}
